﻿using System;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using Manor.CslaDesigner.DslPackage.CustomCode.CodeGeneration;

namespace Manor.CslaDesigner
{
    [global::System.Runtime.InteropServices.Guid("59C63FB4-D9C2-4417-BBD9-92876864F818")]
    
    class CodeGenerator : TemplatedCodeGenerator
    {
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {

            // Replace the supplied file contents with the template we want to run 
            inputFileContent = Encoding.UTF8.GetString(CodeGenerationResource.CslaGenerator);

            // Substitute the name of the current model file into the template. 
            var fi = new FileInfo(inputFileName);
            inputFileContent = inputFileContent.Replace("Diagramm1.csladsl", fi.Name);
            inputFileContent = inputFileContent.Replace("namespace Diagramm1", "namespace " + FileNamespace);

            // Now just delegate the rest of the work to the base class 
            var data = base.GenerateCode(inputFileName, inputFileContent); 
            var ascii = new byte[data.Length - 3]; 
            Array.Copy(data, 3, ascii, 0, data.Length - 3); 

            return ascii;
            
        }

    }
}
